﻿Imports Microsoft.Practices.EnterpriseLibrary.Common.Configuration
Imports System.Data.Common

Public Class UsuarioData
#Region "Declaracion"
    Private _database As Database
#End Region

#Region "Constructor"
    Public Sub New()
        _database = EnterpriseLibraryContainer.Current.GetInstance(Of Database)("Vinum")
    End Sub
#End Region

    Public Function VerificarUsuario(ByVal Usuario As UsuarioObj) As Boolean
        Using Transaccion As New System.Transactions.TransactionScope
            Try
                Dim Comando As DbCommand = _database.GetStoredProcCommand("PaConsultarUsuario")
                Dim Retorno As Boolean

                _database.AddInParameter(Comando, "Opcion", DbType.Int32, 0)
                _database.AddInParameter(Comando, "Login", DbType.String, Usuario.LoginEncriptado)
                _database.AddInParameter(Comando, "Password", DbType.String, Usuario.PasswordEncriptado)

                _database.AddOutParameter(Comando, "PasswordCorrecto", DbType.Boolean, 1)
                _database.AddOutParameter(Comando, "Nombre", DbType.String, 100)
                _database.AddOutParameter(Comando, "Id", DbType.Int32, 100)

                _database.ExecuteNonQuery(Comando)

                Retorno = Convert.ToBoolean(_database.GetParameterValue(Comando, "PasswordCorrecto"))
                Usuario.Id = Convert.ToInt32(_database.GetParameterValue(Comando, "Id"))
                Usuario.Nombre = Convert.ToString(_database.GetParameterValue(Comando, "Nombre"))

                Return Retorno

                Transaccion.Complete()

            Catch ex As SqlException
                Throw ex
            Catch ex As Exception
                Throw New AccesoDatosException(String.Format(My.Resources.ResMensajes.ErrorDesconocido, ex.Message), ex)
            End Try
        End Using
    End Function

End Class
